package com.wmwj.hxlj.mapper;

import com.wmwj.hxlj.entity.CollectionFolder;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface CollectionFolderMapper {

    /**
     * 根据用户id和文件夹名查询是否存在收藏夹
     * @param userId
     * @param folderName
     * @return 查询到的收藏夹对象
     */
    @Select("SELECT * FROM collection_folder WHERE user_id = #{userId} AND folder_name = #{folderName}")
    CollectionFolder findByUserIdAndFolderName(Long userId, String folderName);

    /**
     * 创建收藏夹
     * @param collectionFolder
     * @return 创建成功返回1，失败返回0
     */
    @Insert("INSERT INTO collection_folder(user_id, folder_name, is_private)"+
            " VALUES(#{userId}, #{folderName}, #{isPrivate})")
    int insert(CollectionFolder collectionFolder);

    /**
     * 根据用户id查询该用户所有的收藏夹
     * @param userId
     * @return 收藏夹列表
     */
    @Select("SELECT * FROM collection_folder WHERE user_id = #{userId}")
    List<CollectionFolder> findByUserId(Long userId);

    /**
     * 根据id批量删除收藏夹
     * @param ids
     * @return
     */
    @Delete({"<script>","DELETE FROM collection_folder WHERE id IN" +
                "<foreach collection = 'ids' item = 'id' open='(' separator=',' close=')'>#{id}</foreach>"+
            "</script>"})
    int deleteByIds(@Param("ids") List<Long> ids);

    /**
     * 根据id查询收藏夹是否存在
     * @param id
     * @return
     */
    @Select("SELECT * FROM collection_folder WHERE id = #{id}")
    CollectionFolder findById(Long id);

    /**
     * 根据id更新收藏夹信息
     * @param collectionFolder
     * @return
     */
    @Update("UPDATE collection_folder SET folder_name = #{folderName}, is_private = #{isPrivate} WHERE id = #{id}")
    int update(CollectionFolder collectionFolder);
}
